import deepCopy from "./deepCopy"
import flatten from "./flatten"
function json2formdata(json, isNull = false) {
    var data = flatten(json);
    var formData = new FormData();
    for(let key in data){
        (isNull || data[key]) && formData.append(key, data[key]);
    }
    return formData;
}

function dateFtt(date, fmt = "yyyy-MM-dd HH:mm:ss"){
    var o = {
        "M+": date.getMonth() + 1,
        "d+": date.getDate(),
        "H+": date.getHours(),
        "m+": date.getMinutes(),
        "s+": date.getSeconds(),
        "q+": Math.floor((date.getMonth() + 3) / 3), // 季度
        "S": date.getMilliseconds()
    }
    if(/(y+)/.test(fmt)){
        fmt = fmt.replace(
            RegExp.$1, 
            (date.getFullYear() + "").substr(4 - RegExp.$1.length)
        );
    }
    for(var k in o){
        if(new RegExp("(" + k + ")").test(fmt)){
            fmt = fmt.replace(
            	RegExp.$1, 
                (RegExp.$1.length == 1) ?
                	(o[k]):
                	(("00" + o[k]).substr(("" + o[k]).length))
            );
        }
    }
    return fmt;
}

export {
    deepCopy,
    flatten,
    json2formdata,
    dateFtt
}